package com.suyl.springsession.demo.httpSession.controller;

import com.suyl.springsession.demo.DemoApplication;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
public class HttpSessionController {

    private static Logger logger = LoggerFactory.getLogger(DemoApplication.class);

    @Value("${server.port}")
    Integer port;

    @GetMapping("/set")
    public String set(HttpSession session) {
        logger.info("session set :" + port + "time" + System.currentTimeMillis());
        session.setAttribute("user", "javaboy" + port);
        return String.valueOf(port);
    }

    @GetMapping("/get")
    public String get(HttpSession session) {
        logger.info("session get :" + port + "time" + System.currentTimeMillis());
        return session.getAttribute("user") + ":" + port;
    }
}
